package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.CustomerInquiry;
import com.by.bean.CustomerInquiryDetail;
import com.by.bean.Employee;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.mapper.CustomerInquiryDetailMapper;
import com.by.mapper.EmployeeMapper;
import com.by.query.CustomerInquiryQuery;
import com.by.service.CustomerInquiryDetailService;
import com.by.service.CustomerInquiryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "客户询价单")
@RequestMapping("/api")
public class CustomerInquiryController extends BaseController {

    @Autowired
    CustomerInquiryService customerInquiryService;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    CustomerInquiryDetailMapper customerInquiryDetailMapper;
    @Autowired
    CustomerInquiryDetailService customerInquiryDetailService;

    @ApiOperation("保存询价单")
    @PostMapping("/customerInquiry")
    public ResultBean save(@RequestBody CustomerInquiry customerInquiry) {
        int rs = customerInquiryService.save(customerInquiry);
        return saveResult(rs);
    }

    @ApiOperation("询价单列表")
    @PostMapping("/customerInquiry/list")
    public ResultBean<PageBean> query(@RequestBody CustomerInquiryQuery query) {
        PageBean pageBean = customerInquiryService.queryByMap(BeanUtil.beanToMap(query));
        return success("询价单列表", pageBean);
    }


    @ApiOperation("询价详情")
    @GetMapping("/customerInquiry")
    public ResultBean get(@RequestParam(required = true) int id) {
        CustomerInquiry inquiry = customerInquiryService.get(id);
        return success("询价详情", inquiry);
    }

    @ApiOperation("查询员工信息")
    @GetMapping("/employee/list")
    public ResultBean getEmployeeList() {
        List<Employee> list = employeeMapper.querySales();
        return success("查询员工信息成功",list);
    }

    /*@ApiOperation("查询客户产品信息")
    @GetMapping("/customer/detail/{id}")
    public ResultBean get(@PathVariable Integer id,
                          @RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "10") int pageSize) {
        // 封装查询条件
        Map<String, Object> params = new HashMap<>();
        params.put("inquiryId", id); // 询价单ID
        params.put("page", page); // 当前页
        params.put("pageSize", pageSize); // 每页记录数

        // 调用 queryByMap 方法
        PageBean pageBean = customerInquiryDetailService.queryByMap(params);
        return success("查询客户产品信息成功", pageBean);
    }*/
    @ApiOperation("查询客户产品信息")
    @GetMapping("/customer/detail/{inquiryId}/{page}/{pageSize}")
    public ResultBean get(@PathVariable Integer inquiryId,
                          @PathVariable int page,
                          @PathVariable int pageSize) {
        // 使用 PageHelper 分页

        PageHelper.startPage(page, pageSize);
        List<Map> list = customerInquiryDetailMapper.selectByInquiryIdWithPage(inquiryId);
        PageInfo<Map> info = new PageInfo<>(list);

        // 返回分页信息
        PageBean pageBean = new PageBean(page, pageSize, info.getTotal(), info.getList());
        return success("查询客户产品信息成功", pageBean);
    }
}
